package com.example.opengldemo._111_;

import com.example.opengldemo.util.MatrixState;

public class TreeSingle implements Comparable<TreeSingle> {

    float x , z ;
    float cx , cz ;
    float yAngle ;
    TreeGroup  group ;

    public TreeSingle(float x, float z, float yAngle, TreeGroup group) {
        this.x = x;
        this.z = z;
        this.yAngle = yAngle;
        this.group = group;
    }

    public void drawSelf(int texId){
        MatrixState.pushMatrix();
        MatrixState.translate(x, 0, z);
        MatrixState.rotate(yAngle,0,1,0);
        group.tfd.drawSelf(texId);
        MatrixState.popMatrix();
    }

    public void calculateBillboardDirection(){
        float xspan=x-cx;
        float zspan=z-cz;
        if(zspan <=  0){
            yAngle=(float)Math.toDegrees(Math.atan(xspan/zspan));
        }else{
            yAngle=180+(float)Math.toDegrees(Math.atan(xspan/zspan));
        }

    }
    @Override
    public int compareTo(TreeSingle treeSingle) {
        float xs=x-cx;
        float zs=z-cz;

        float xo=treeSingle.x-cx;
        float zo=treeSingle.z-cz;

        float disA=(float)Math.sqrt(xs*xs+zs*zs);
        float disB=(float)Math.sqrt(xo*xo+zo*zo);

        return ((disA-disB)==0)?0:((disA-disB)>0)?-1:1;
    }
}
